package com.rmondjone.androidcamerademo

import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import android.view.View
import android.widget.TextView
import android.widget.Toast
import androidx.recyclerview.widget.LinearLayoutManager
import com.ant.http.Bean.HttpResult
import com.ant.http.HttpManager
import com.ant.http.HttpManager.getService
import com.blankj.utilcode.util.LogUtils
import com.rmondjone.androidcamerademo.bean.CameraType
import com.rmondjone.androidcamerademo.bean.HomeBean
import com.rmondjone.camera.BaseActivity
import com.rmondjone.camera.PermissionUtils
import com.rmondjone.camera.PermissionUtils.PermissionListener
import com.rmondjone.face.ViewModel.OnCallBack
import com.rmondjone.utils.StatusBarUtil
import com.rmondjone.utils.filterRepeatClick
import com.rmondjone.utils.setClickListener
import com.rmondjone.view.NotifyDialog
import com.yanzhenjie.permission.AndPermission
import com.yanzhenjie.permission.Permission
import com.zzhoujay.richtext.RichText
import kotlinx.android.synthetic.main.activity_face.*
import kotlinx.android.synthetic.main.activity_face.recyclerview
import kotlinx.android.synthetic.main.activity_main.*
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

/**
 * copyright：
 * @author：anthui creation date： .
 * @since: 1.0.0
 * @describe:
 */
class MainActivity : BaseActivity() {


    companion object {

        var isClick = false

        fun startActivity(context: Context, type: Int) {

            if (isClick) {
                return
            }
            isClick = true
            val intent = Intent(context, PhotoActivity::class.java)
            intent.putExtra("type", type)
            context.startActivity(intent)
        }

        var data: List<TypeBean>? = null
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        StatusBarUtil.setStatusBarDarkFontMode(this, Color.TRANSPARENT)
        findViewById<View>(R.id.layout_face).setClickListener {
//            if (true) {
//
//
//                TipsDialog().show(supportFragmentManager)
//                return@setClickListener
//            }

            gotoCamare(CameraType.CAMERA_TYPE_FMIAN_ZHEN)
        }
        layout_home.setClickListener {  }
        findViewById<View>(R.id.layout_she).setClickListener {
            gotoCamare(CameraType.CAMERA_TYPE_SHE_ZHEN)
        }
        findViewById<View>(R.id.layout_xue).setClickListener { gotoCamare(CameraType.CAMERA_TYPE_XUE_WEI) }
        initData()
//        addText();
    }


    private fun addText() {
        val viewById = findViewById<TextView>(R.id.tv_content)
        val msg = "将艾帮主蕲艾敷贴贴在相应位置或使用艾帮主筋骨痛在相应穴位滑动\r\n<span style='text-align:center;font-weight:bold'>中脘穴</span>\r\n<img src=\"https://baikeimg.aibangzhu888.com/acupoint/1500884160337.jpg\" width=\"80%\">\r\n<span style='text-align:center;font-weight:bold'>丰隆穴</span>\n\n<img src=\"https://baikeimg.aibangzhu888.com/acupoint/1500364754226.jpg\" width=\"80%\">\r\n"
        RichText.from(msg).into(viewById);


//        viewById.text = Html.fromHtml(msg, MyImageGetter(viewById, resources)
//                , null)
    }

    override fun onResume() {
        super.onResume()
        isClick = false
        data = null
    }

    var data: List<TypeBean>? = null

    /**
     * 注释：跳转拍照
     * 时间：2019/3/21 0021 9:13
     * 作者：郭翰林
     *
     * @param cameraTypeFmianZhen
     */
    fun gotoCamare(cameraTypeFmianZhen: Int) {
        PermissionUtils.applicationPermissions(this, object : PermissionListener {
            override fun onSuccess(context: Context) {
                if (cameraTypeFmianZhen == CameraType.CAMERA_TYPE_XUE_WEI) {
                    getService(CommonService::class.java).seetype().enqueue(object : Callback<HttpResult<List<TypeBean>>> {
                        override fun onResponse(call: Call<HttpResult<List<TypeBean>>>, response: Response<HttpResult<List<TypeBean>>>) {
                            if (response.body() != null && response.body()!!.code == 1 && response.body()!!.data != null) {
                                MainActivity.data = response.body()!!.data
                                startActivity(this@MainActivity, cameraTypeFmianZhen)
                            } else {
                                Toast.makeText(context, response.body()?.message
                                        ?: "请求失败", Toast.LENGTH_SHORT).show()
                            }
                        }

                        override fun onFailure(call: Call<HttpResult<List<TypeBean>>>, t: Throwable) {

                            Toast.makeText(context, "请求失败", Toast.LENGTH_SHORT).show()

                        }
                    })
                } else {
                    startActivity(this@MainActivity, cameraTypeFmianZhen)
                }
            }

            override fun onFailed(context: Context) {
                if (AndPermission.hasAlwaysDeniedPermission(context, *Permission.Group.CAMERA)
                        && AndPermission.hasAlwaysDeniedPermission(context, *Permission.Group.STORAGE)) {
                    AndPermission.with(context).runtime().setting().start()
                }
                Toast.makeText(context, context.getString(R.string.permission_camra_storage), Toast.LENGTH_SHORT)
            }
        }, Permission.Group.STORAGE, Permission.Group.CAMERA)
    }

    lateinit var homeAdapter: HomeAdapter
    private fun initData() {
        LogUtils.e("msg--------- 0000000")


        recyclerview.apply {

            var linearLayoutManager = LinearLayoutManager(this@MainActivity)
            linearLayoutManager.orientation = LinearLayoutManager.HORIZONTAL
            layoutManager = linearLayoutManager
            homeAdapter = HomeAdapter()
            adapter = homeAdapter
        }
        homeAdapter.setOnItemClickListener { adapter, view, position ->
            view.filterRepeatClick {

                homeAdapter.getItem(position)?.let {

                    click(it)
                }
            }

        }


        HttpManager.getService(CommonService::class.java).home().enqueue(object : OnCallBack<List<HomeBean>>() {


            override fun onFailure(code: Int, msg: String?, er: Throwable?) {

                LogUtils.e("msg-------------- " + msg)
            }

            override fun onSuccess(item: List<HomeBean>) {

                homeAdapter.setNewData(item)
            }

        })
    }

    private fun click(homeBean: HomeBean) {

        when (homeBean.name) {
            "面诊" -> {
                gotoCamare(CameraType.CAMERA_TYPE_FMIAN_ZHEN)
            }
            "舌诊" -> {
                gotoCamare(CameraType.CAMERA_TYPE_SHE_ZHEN)
            }
            "穴位识别" -> {
                gotoCamare(CameraType.CAMERA_TYPE_XUE_WEI)
            }
            else -> {
                NotifyDialog().apply {
                    list.clear()
//                    list.addAll(homeBean.notice)
                    list.addAll(homeBean.notice)
//                    list .addAll(homeBean.notice)

                    show(supportFragmentManager)
                }

            }
        }

    }

}